<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form ref="queryForm" :model="queryForm" label-width="100px" :inline="true" border>
        <el-form-item label="任务名称：" prop="jobName">
          <el-input
            v-model="queryForm.jobName"
            placeholder="请输入任务名称"
            prefix-icon="el-icon-search"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            v-if="btnPermission('job:query:list')"
            type="success"
            size="mini"
            icon="el-icon-search"
            plain
            @click="searchData"
          >查询
          </el-button>
          <el-button
            v-if="btnPermission('job:query:list')"
            type="warning"
            size="mini"
            icon="el-icon-refresh-left"
            plain
            @click="resetQueryFrom"
          >重置
          </el-button>
        </el-form-item>
      </el-form>
      <el-row style="margin-bottom: 5px;">
        <el-col>
          <el-button
            v-if="btnPermission('job:add:entity')"
            :disabled="addDisabled"
            icon="el-icon-circle-plus-outline"
            plain
            size="mini"
            type="primary"
            @click="add"
          >
            新增
          </el-button>
          <el-button
            v-if="btnPermission('job:edit:entity')"
            :disabled="editDisabled"
            icon="el-icon-edit"
            plain
            size="mini"
            type="success"
            @click="edit"
          >
            编辑
          </el-button>
          <el-button
            v-if="btnPermission('job:del:entity')"
            :disabled="editDisabled"
            icon="el-icon-circle-close"
            plain
            size="mini"
            type="danger"
            @click="del"
          >删除
          </el-button>
          <el-button
            v-if="btnPermission('schedule:job:add')"
            type="warning"
            :disabled="editDisabled"
            icon="el-icon-video-play"
            plain
            size="mini"
            @click="start"
          >启动
          </el-button>
          <el-button
            v-if="btnPermission('schedule:job:pause')"
            type="info"
            :disabled="editDisabled"
            icon="el-icon-video-pause"
            plain
            size="mini"
            @click="pause"
          >
            暂停
          </el-button>
          <el-button
            v-if="btnPermission('schedule:job:resume')"
            type="success"
            :disabled="editDisabled"
            icon="el-icon-refresh-left"
            plain
            size="mini"
            @click="resume"
          >
            恢复
          </el-button>
        </el-col>
      </el-row>
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        stripe
        row-key="id"
        border
        :header-cell-style="{textAlign: 'center'}"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          align="center"
          type="selection"
          width="65"
        />
        <el-table-column
          type="index"
          label="序号"
          width="60"
          :index="indexMethod"
          align="center"
        />
        <el-table-column
          prop="jobName"
          label="任务名称"
        />
        <!--        <el-table-column-->
        <!--          prop="jobDescription"-->
        <!--          label="任务描述"-->
        <!--        />-->
        <el-table-column
          prop="nameSpace"
          label="命名空间"
          width="300"
        />
        <el-table-column
          prop="jobImplement"
          label="实现类"
        />
        <el-table-column
          prop="cronExpression"
          label="Cron表达式"
        />
        <el-table-column
          prop="order"
          label="排序"
          align="center"
          width="60"
        />
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="160"
        >
          <template slot-scope="scope">
            <span>{{ $moment(scope.row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="jobStatus"
          label="任务状态"
        >
          <template slot-scope="scope">
            <span>{{ JobStatus.convertJobStatus(scope.row.jobStatus) }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          :current-page="queryCondition.PageIndex"
          :page-sizes="[10,15, 20,40,60,80,100]"
          :page-size="queryCondition.PageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="queryCondition.TotalCount"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
      <el-dialog :title="title" :visible.sync="dialogFormVisible" class="demo-ruleForm">
        <el-card class="box-card">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="left">
            <el-row>
              <el-col span="12">
                <el-form-item label="任务名称" prop="JobName">
                  <el-input
                    v-model="form.jobName"
                    prefix-icon="el-icon-search"
                    placeholder="请输入任务名称"
                    class="colWidth"
                  />
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="任务描述" prop="JobDescription">
                  <el-input
                    v-model="form.jobDescription"
                    prefix-icon="el-icon-search"
                    placeholder="请输入任务描述"
                    class="colWidth"
                  />
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="命名空间" prop="NameSpace">
                  <el-input
                    v-model="form.nameSpace"
                    prefix-icon="el-icon-search"
                    placeholder="请输入命名空间"
                    class="colWidth"
                  />
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="实现类" prop="JobImplement">
                  <el-input
                    v-model="form.jobImplement"
                    prefix-icon="el-icon-search"
                    placeholder="请输入实现类"
                    class="colWidth"
                  />
                </el-form-item>
              </el-col>
              <el-col span="24">
                <el-form-item label="Cron表达式" prop="CronExpression">
                  <el-input
                    v-model="form.cronExpression"
                    prefix-icon="el-icon-search"
                    placeholder="Cron表达式"
                    class="colWidth"
                  />
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="开始时间" prop="StartTime">
                  <el-date-picker
                    v-model="form.startTime"
                    type="datetime"
                    placeholder="请选择开始时间"
                    class="colWidth"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="结束时间" prop="EndTime">
                  <el-date-picker
                    v-model="form.endTime"
                    type="datetime"
                    placeholder="请选择结束时间"
                    class="colWidth"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="序号" prop="Order">
                  <el-input-number
                    v-model="form.order"
                    :min="0"
                    placeholder="请输入序号"
                    label="请输入序号"
                    class="colWidth"
                  />
                </el-form-item>
              </el-col>
              <el-col span="12">
                <el-form-item label="备注" prop="Remark">
                  <el-input
                    v-model.number="form.remark"
                    type="text"
                    prefix-icon="el-icon-search"
                    placeholder="请输入备注"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
        <div slot="footer" class="dialog-footer">
          <el-button icon="el-icon-circle-check" plain size="mini" type="success" @click="submitForm">确 定
          </el-button>
<!--          <el-button icon="el-icon-refresh-left" plain size="mini" type="warning" @click="resetForm">重 置</el-button>-->
          <el-button icon="el-icon-close" plain size="mini" type="danger" @click="dialogFormVisible = false">取 消
          </el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import BasePage from '@/components/BasePage/BasePage'
import { generateQueryItem } from '@/utils/query-item'
import { Guid } from '@/constants/guid'
import { DateTime } from '@/constants/date'

export default {
  name: 'ScheduleJobManagement',
  extends: BasePage,
  data() {
    return {
      queryForm: {
        jobName: null
      },
      form: {
        id: null,
        jobName: null,
        jobDescription: null,
        nameSpace: null,
        jobImplement: null,
        cronExpression: null,
        startTime: null,
        endTime: null,
        order: null,
        remark: null,
        isDel: null,
        creator: null,
        createTime: null,
        modifier: null,
        modifyTime: null
      },
      rules: {
        jobName: [
          { required: true, message: '请输入任务名称', trigger: 'blur' },
          { min: 0, max: 128, message: '长度在 0 到 128 个字符', trigger: 'blur' }
        ],
        jobDescription: [
          { min: 0, max: 512, message: '长度在 0 到 512 个字符', trigger: 'blur' }
        ],
        nameSpace: [
          { required: true, message: '请输入命名空间', trigger: 'blur' },
          { min: 0, max: 512, message: '长度在 0 到 512 个字符', trigger: 'blur' }
        ],
        jobImplement: [
          { required: true, message: '请输入实现类', trigger: 'blur' },
          { min: 0, max: 128, message: '长度在 0 到 128 个字符', trigger: 'blur' }
        ],
        cronExpression: [
          { required: true, message: '请输入cron表达式', trigger: 'blur' },
          { min: 0, max: 128, message: '长度在 0 到 128 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {},
  created() {
    // window.addEventListener('storage', this.afterQRScan)
    this.url.queryList = 'ScheduleJobManagement/Post'
    this.url.queryEntity = 'ScheduleJobManagement/GetById/'
    this.url.addEntity = 'ScheduleJobManagement/Add'
    this.url.editEntity = 'ScheduleJobManagement/Put'
    this.url.deleteEntity = 'ScheduleJobManagement/Delete/'
    this.queryCondition.Sort = 'X.Order ASC'
  },
  mounted() {
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    searchData: function() {
      this.queryCondition.PageIndex = 0
      this.queryCondition.QueryItems = []
      if (this.queryForm.jobName) {
        this.queryCondition.QueryItems.push(generateQueryItem(
          'JobName',
          this.DataType.String,
          this.QueryMethod.Like,
          this.queryForm.jobName))
      }
      this.initData()
    },
    add: function() {
      this.resetForm()
      this.title = '新增任务'
      this.form.id = Guid.Empty
      this.form.isDel = 0
      this.form.creator = Guid.Empty
      this.form.createTime = DateTime.Default
      this.form.modifier = Guid.Empty
      this.form.modifyTime = DateTime.Default
      this.dialogFormVisible = true
    },
    edit: function() {
      this.loading = true
      this.resetForm()
      this.getEntity(this.multipleSelection[0].id).then(response => {
        this.title = '编辑任务'
        Object.assign(this.form, response.object)
        this.form.jobName = this.multipleSelection[0].jobName
        this.dialogFormVisible = true
        this.loading = false
      })
    },
    resetForm() {
      this.form.id = null
      this.form.jobName = null
      this.form.jobDescription = null
      this.form.nameSpace = null
      this.form.jobImplement = null
      this.form.cronExpression = null
      this.form.startTime = null
      this.form.endTime = null
      this.form.order = null
      this.form.remark = null
      this.form.isDel = null
      this.form.creator = null
      this.form.createTime = null
      this.form.modifier = null
      this.form.modifyTime = null
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    },
    start: function() {
      const row = this.multipleSelection[0]
      if (row.jobStatus === this.JobStatus.Running) {
        this.$message({ showClose: true, message: '正在运行', type: 'warning' })
        return
      }
      if (row.jobStatus === this.JobStatus.Stoped) {
        this.$message({ showClose: true, message: '请恢复', type: 'warning' })
        return
      }
      this.get('ScheduleJobManagement/StartScheduleJob/' + row.id, null).then(response => {
        this.$message({ showClose: true, message: '启动成功', type: 'success' })
        this.editDisabled = true
        this.delDisabled = true
        this.initData()
      })
    },
    pause: function() {
      const row = this.multipleSelection[0]
      if (row.jobStatus !== this.JobStatus.Running) {
        this.$message({ showClose: true, message: '未在运行', type: 'warning' })
        return
      }
      this.get('ScheduleJobManagement/PauseScheduleJob/' + row.id, null).then(response => {
        this.$message({ showClose: true, message: '暂停成功', type: 'success' })
        this.editDisabled = true
        this.delDisabled = true
        this.initData()
      })
    },
    resume: function() {
      const row = this.multipleSelection[0]
      if (row.jobStatus !== this.JobStatus.Stoped) {
        this.$message({ showClose: true, message: '未停止', type: 'warning' })
        return
      }
      this.get('ScheduleJobManagement/ResumeScheduleJob/' + row.id, null).then(response => {
        this.$message({ showClose: true, message: '恢复成功', type: 'success' })
        this.editDisabled = true
        this.delDisabled = true
        this.initData()
      })
    }
  }
}
</script>

<style scoped>

</style>
